const mysql=require('mysql');
async function sqlinit(){
    var db=mysql.createPool({
        host:"localhost",
        port:3306,
        user:"root",
        password:"root",
        database:"game"
    });
   return await getConnection(db); 
}

function getConnection(db){
    return new Promise(function(resolve,reject){
        db.getConnection(function(err,connection){
            if(err) resolve(null);
            else resolve(connection);
        })
    })
}


function selectAll(connect){
    return new Promise(function(resolve,reject){
      connect.query("SELECT * FROM `list` WHERE 1",function(err,res){
          if(err) resolve(null);
          else resolve(res)
      })
    })
  }
// 注册
  function insertOne(connect,data){
    return new Promise(function(resolve,reject){
        var arr=Object.values(data)
        connect.query("INSERT INTO `list`(`phone`, `password`) VALUES (?,?)",arr,function(err,res){
            if(res) resolve(true);
            else resolve(false)
        })
    })
}
// 登录
function selectOne(connect, data) {
    return new Promise(function (resolve, reject) {
        // 去数据库里查找，并判断
        connect.query("SELECT * FROM `list` WHERE `phone` = '" + data.phone + "' AND `password` = '" + data.password + "'", function (err, res) {
            if (res && res.length !== 0) resolve(true);
            else resolve(false);
        })
    })
}
// 列表

function shoppingAll(connect){
    return new Promise(function(resolve,reject){
      connect.query("SELECT * FROM `list-shopping` WHERE 1",function(err,res){
          if(err) resolve(null);
          else resolve(res)
      })
    })
  }
//   详情
function xiangqingAll(connect){
    return new Promise(function(resolve,reject){
      connect.query("SELECT * FROM `xiangqing` WHERE 1",function(err,res){
          if(err) resolve(null);
          else resolve(res)
      })
    })
  }
  //添加购物车
  function selectShopping(connect, data) {
    return new Promise(function (resolve, reject) {
        var arr = Object.values(data);
        connect.query("SELECT * FROM `shopping` WHERE `pid` = '" + data.pid + "'", function (err, res) {
            if (res.length === 0) {
                connect.query("INSERT INTO `shopping`(`pid`,`ck`, `img`, `txt`, `price`, `num`, `total`,`remove`) VALUES (?,?,?,?,?,?,?,?)", arr, function (err, res) {
                    if (res) resolve(true);
                    else resolve(false);
                })
            } else {
                connect.query("UPDATE `shopping` SET `num`='" + data.num + "' WHERE `pid`='" + data.pid + "'", function (err, res) {
                    if (res) resolve(true);
                    else resolve(false);
                })
            }
        })
    })
}
// 购物车页面查找shopping表单
function ShoppingList(connect) {
    return new Promise(function (resolve, reject) {
        // 去数据库里查找，并返回
        connect.query("SELECT * FROM `shopping` WHERE 1", function (err, res) {
            if (err) {
                resolve(null)
            } else {
                resolve(res)
            }
        })
    })
}
// 通过传进来的id删除对应的数据
function selectremove(connect, data) {
    return new Promise(function (resolve, reject) {
        connect.query("DELETE  FROM `shopping` WHERE `pid` = '" + data + "'", function (err, res) {
            if (res) resolve(true);
            else resolve(false);
        })
    })
}


// 购物车界面修改数量
function selectChangeNum(connect, data) {
    return new Promise(function (resolve, reject) {
        connect.query("UPDATE `shopping` SET `num`='" + data.step + "' WHERE `pid`='" + data.pid + "'", function (err, res) {
            if (res) resolve(true);
            else resolve(false);
        })
    })
}

function selectQK(connect){
    return new Promise(function (resolve, reject) {
        connect.query("DELETE FROM `shopping` WHERE 1", function (err, res) {
            if (err) {
                resolve(null)
            } else {
                resolve(res)
            }
        })
    })
}

exports.selectAll=selectAll;
exports.insertOne=insertOne;
exports.selectOne=selectOne
exports.shoppingAll=shoppingAll;
exports.xiangqingAll=xiangqingAll;
exports.sqlinit=sqlinit;
exports.selectShopping=selectShopping;
exports.ShoppingList=ShoppingList;
exports.selectremove=selectremove;
exports.selectChangeNum=selectChangeNum;
exports.selectQK=selectQK